#########################
# Stefan Wojcik
# Creating figures in PSRM paper, 'Why Legislative Networks? Analyzing Legislative Network Formation'
###############################################
#install.packages("sna")
library(network)
library(sna)
library(ergm)
library(stargazer)

# File containing the network objects
load("plottedNetworks.RData")

## DESCRIPTIVES - connectedness across parties and states - barplots
p.deg.mat <- matrix(NA, ncol=7, nrow=length(unique(net.full%v%"party")))
p.deg.mat[,1] <- unique(net.full%v%"party")
for(i in 1:nrow(p.deg.mat)){
  com.in <- mean(degree(netcom, cmode="indegree", nodes=netcom%v%"party"==p.deg.mat[i,1]), na.rm=T)
  com.out <- mean(degree(netcom, cmode="outdegree", nodes=netcom%v%"party"==p.deg.mat[i,1]), na.rm=T)
  soc.in <- mean(degree(netsoc, cmode="indegree", nodes=netsoc%v%"party"==p.deg.mat[i,1]), na.rm=T)
  soc.out <- mean(degree(netsoc, cmode="outdegree", nodes=netsoc%v%"party"==p.deg.mat[i,1]), na.rm=T)
  info.in <- mean(degree(netinfo, cmode="indegree", nodes=netinfo%v%"party"==p.deg.mat[i,1]), na.rm=T)
  info.out <- mean(degree(netinfo, cmode="outdegree", nodes=netinfo%v%"party"==p.deg.mat[i,1]), na.rm=T)
  p.deg.mat[i, -1] <- c(com.in, com.out, soc.in, soc.out, info.in, info.out)
} 
#Indegree - FIGURE 2
in.mat <- apply(p.deg.mat[,2:7], 2, function(x) as.numeric((as.character(x))))
in.mat <- apply(in.mat, 2, function(x) ifelse(is.na(x), 0, x))
o1 <- order(in.mat[,1], decreasing=T)
par(mar=c(3,3,3,4))
barplot(t(in.mat[o1,c(1,3,5)]), names=p.deg.mat[o1,1], cex.names=.5, las=2, xlab="Party", ylab="Average Indegree", main="Incoming Links by Party",beside=T)
legend(x=40,y=3, cex=.6, legend=c("Communication", "Social","Info. Seeking"), fill=c("black", "dark grey", "light grey", "white"))
# use locator(n=1) to put the legend in a more convenient spot
#Outdegree - FIGURE 3
out.mat <- apply(p.deg.mat[,2:7], 2, function(x) as.numeric((as.character(x))))
out.mat <- apply(in.mat, 2, function(x) ifelse(is.na(x), 0, x))
o1 <- order(in.mat[,2], decreasing=T)
barplot(t(in.mat[o1,c(2,4,6)]), names=p.deg.mat[o1,1], cex.names=.5, las=2, xlab="Party", ylab="Average Outdegree", main="Outgoing Links by Party",beside=T)
legend(x=40,y=6, cex=.6, legend=c("Communication", "Social","Info. Seeking"), fill=c("black", "dark grey", "light grey", "white"))



# NETWORK PLOT OF COMMUNICATION TIES - FIGURE 4
indeg<- degree(netcom, cmode="indegree")
odeg<- degree(netcom, cmode="outdegree")
io <- indeg+odeg
par(mar=c(2,2,2,2))
names <- netcom %v% "party"
ramp <- colorRampPalette(c("yellow", "red", "blue"))
cols <- ramp(21)
party <- as.numeric(as.factor(netcom%v%"party"))
col.vec <- rep(NA, length(names))
for(i in 1:length(cols)){
  col.vec[names%in%unique(names)[i]] <- cols[i]
  #Unique color 1 occupies all of unique party 1
}
par(bg="white")
plot(netcom, vertex.col=col.vec, vertex.cex=sqrt(io)/2, edge.lwd=.2, arrowhead.cex=.5, edge.col="gray25", col.lab="white", main="")
legend("topright", legend=unique(names), fill=unique(col.vec), border="grey", cex=.6, text.width=11, text.col="black")

##################
#SOCIAL 
#####################

#PLOT OF SOCIAL NETWORK - FIGURE 5
indeg<- degree(netsoc, cmode="indegree")
odeg<- degree(netsoc, cmode="outdegree")
io <- indeg+odeg
names <- netsoc %v% "party"
ramp <- colorRampPalette(c("yellow", "red", "blue"))
cols <- ramp(length(unique(names)))
party <- as.numeric(as.factor(netsoc%v%"party"))
col.vec <- rep(NA, length(names))
for(i in 1:length(cols)){
  col.vec[names%in%unique(names)[i]] <- cols[i]
  #Unique color 1 occupies all of unique party 1
}
plot(netsoc, vertex.col=col.vec, vertex.cex=sqrt(io)/2, edge.lwd=.2, arrowhead.cex=.5, edge.col="gray25", main="")
legend("topright", legend=unique(names), fill=unique(col.vec), border="grey", cex=.7, text.width=11, text.col="black")

#Averages...
fd <-  mean(degree(netcom, nodes=netcom%v%"party"=="DEM"), na.rm=T)

##############
# INFORMATION SEEKING
################

#PLOT OF INFORMATION SEEKING NETWORK - FIGURE 6
indeg<- degree(netinfo, cmode="indegree")
odeg<- degree(netinfo, cmode="outdegree")
io <- indeg+odeg
names <- netinfo %v% "party"
ramp <- colorRampPalette(c("yellow", "red", "blue"))
cols <- ramp(length(unique(names)))
party <- as.numeric(as.factor(netinfo%v%"party"))
col.vec <- rep(NA, length(names))
for(i in 1:length(cols)){
  col.vec[names%in%unique(names)[i]] <- cols[i]
  #Unique color 1 occupies all of unique party 1
}

plot(netinfo, vertex.col=col.vec, vertex.cex=sqrt(io)/2, edge.lwd=.2, arrowhead.cex=.5, edge.col="gray25", main="Brazilian Information-seeking Network")
legend("topright", legend=unique(names), fill=unique(col.vec), border="grey", cex=.7, text.width=11, text.col="black")


